package org.display.util
{
	import flash.events.*;
	import flash.filesystem.*;
	import flash.net.*;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	
	import org.abcFrame.core.AbcEventDispatcher;
	import org.data.Global;
	import org.events.AbcNetEvent;
	import org.utils.*;
	
	public class Upload
	{
		
		private var path:String
		
		public function Upload(path:String,http:String)
		{
			//file:///D:/abcFF/xml/org/display/aishi/TestView.xml
			
			this.path=path
			
		    var path:String = path.replace(/file:\/\/\//gis,"")
			
			//设置保存文件的本地路径，如果不存在则新建一个 
			var files:File =File.desktopDirectory.resolvePath(path); 
			var byt:ByteArray=new ByteArray()
			var stream:FileStream = new FileStream(); 
			stream.open(files, FileMode.READ); 
			
			stream.readBytes(byt,0,stream.bytesAvailable);  
			
			stream.close()
				
			//trace(RegControl.transform(Global.workspace.replace(/\\/gis,"/")+"\/"))
				
			var reg:RegExp = new RegExp( RegControl.transform(Global.workspace.replace(/\\/gis,"/")+"\/"),"gis" )
			
			path=path.replace(/\\/gis,"/").replace(reg,"")

			upload(byt,path,http)
			
		}
		
		function upload(bytes:ByteArray,path:String,http:String){
			
			var request:URLRequest = new URLRequest(http+"?path="+path); 
			request.contentType="application/octet-stream" 
			
			request.data =bytes
			
			request.method = URLRequestMethod.POST 
			var urlloader:URLLoader=new URLLoader() 
			urlloader.addEventListener(Event.COMPLETE,complete) 
			urlloader.addEventListener(IOErrorEvent.IO_ERROR, Err_event) 
			//urlloader.addEventListener(ProgressEvent.PROGRESS ,progressHandler) 
			urlloader.load(request) 
				
		}
		
		function complete(e:Event){

			AbcEventDispatcher.instance.dispatcher(AbcNetEvent.UploadFile,URLLoader(e.target).data)

		}
		
		function Err_event(e:IOErrorEvent){
		
			Alert.yesLabel = "Yes";
			Alert.noLabel = "No";
			Alert.show("上传中断，是否继续?", "", 3, null, alertClickHandler);
			
		}
		
		function alertClickHandler(event:CloseEvent){
		
			if (event.detail==Alert.YES)
				AbcEventDispatcher.instance.dispatcher(AbcNetEvent.UploadFile,path)
			
		
		}
	}
}